package com.kkb.controller;

import com.kkb.domain.AjaxResult;
import com.kkb.pojo.TbProduct;
import com.kkb.pojo.TbSpec;
import com.kkb.service.TbProductService;
import com.kkb.service.TbSpecService;
import io.swagger.annotations.Api;
import io.swagger.annotations.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品
 */
@Api()
@Controller
@RequestMapping("/TbProduct")
public class TbProductController {

    @Autowired
    private TbProductService tbProductService;

    @Autowired
    private TbSpecService tbSpecService;

    @ResponseBody
    @RequestMapping("/all")
    public List<TbProduct> getAll(){
        List<TbProduct> all = tbProductService.getAll();
        return all;
    }

    /**
     * 添加商品
     * @param tbProduct
     * @return
     */
    @ResponseBody
    @RequestMapping("/addTbProduct")
    public AjaxResult addTbProduct(@RequestBody TbProduct tbProduct){
        int result=tbProductService.addTbProduct(tbProduct);
        if(result<1){
            return AjaxResult.error("添加商品失败！");
        }
        return AjaxResult.success();
    }

    /**
     * 查询商品页面的所有规格名称
     * @return
     */
    @ResponseBody
    @RequestMapping("/querySpecList")
    public AjaxResult querySpecList(){
        List<TbSpec> specList=tbSpecService.querySpecList();
        return AjaxResult.success(specList);
    }

    /**
     * 新建规格，保存至规格表及规格选项表
     * @param tbSpec
     * @return
     */
    @ResponseBody
    @RequestMapping("/addSpecAndOption")
    public AjaxResult addSpecAndOption(@RequestBody TbSpec tbSpec){
        AjaxResult result=tbSpecService.addSpecAndOption(tbSpec);
        return result;
    }

}
